{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Number of Good Pairs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #math #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #数学 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: numIdenticalPairs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #好数对的数目"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组 <code>nums</code> 。</p>\n",
    "\n",
    "<p>如果一组数字 <code>(i,j)</code> 满足 <code>nums[i]</code> == <code>nums[j]</code> 且 <code>i</code> &lt; <code>j</code> ，就可以认为这是一组 <strong>好数对</strong> 。</p>\n",
    "\n",
    "<p>返回好数对的数目。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [1,2,3,1,1,3]\n",
    "<strong>输出：</strong>4\n",
    "<strong>解释：</strong>有 4 组好数对，分别是 (0,3), (0,4), (3,4), (2,5) ，下标从 0 开始\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [1,1,1,1]\n",
    "<strong>输出：</strong>6\n",
    "<strong>解释：</strong>数组中的每组数字都是好数对</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [1,2,3]\n",
    "<strong>输出：</strong>0\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 100</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [number-of-good-pairs](https://leetcode.cn/problems/number-of-good-pairs/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [number-of-good-pairs](https://leetcode.cn/problems/number-of-good-pairs/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,3,1,1,3]', '[1,1,1,1]', '[1,2,3]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numIdenticalPairs(self, nums: List[int]) -> int:\n",
    "        \n",
    "        res = 0\n",
    "        for i in range(len(nums)-1):\n",
    "            for j in range(i+1,len(nums)):\n",
    "                if nums[i] == nums[j]:\n",
    "                    res += 1\n",
    "        return res\n",
    "                \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numIdenticalPairs(self, nums: List[int]) -> int:\n",
    "        n = 0\n",
    "        for i in range(len(nums)-1):\n",
    "            for j in range(i+1, len(nums)):\n",
    "                if nums[i] == nums[j]:\n",
    "                    n += 1\n",
    "        return n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numIdenticalPairs(self, nums: List[int]) -> int:\n",
    "        count = 0\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i + 1, len(nums)):\n",
    "                if nums[i] == nums[j]:\n",
    "                    count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numIdenticalPairs(self, nums: List[int]) -> int:\n",
    "        old = []\n",
    "        sums = 0\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] not in old:\n",
    "                n = nums.count(nums[i])\n",
    "                sums += n*(n-1)//2\n",
    "                old.append(nums[i])\n",
    "            else:\n",
    "                pass\n",
    "        return sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numIdenticalPairs(self, nums: List[int]) -> int:\n",
    "        count = 0\n",
    "        testset = set(nums)\n",
    "        for i in range(len(testset )):\n",
    "            target = nums[0]\n",
    "            testcount = nums.count(nums[0])\n",
    "            for j in range(testcount):\n",
    "                nums.remove(target)\n",
    "            if testcount > 1:\n",
    "                count +=  testcount * ( testcount - 1 ) / 2\n",
    "        return int(count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numIdenticalPairs(self, nums: List[int]) -> int:\n",
    "        k = len(nums)\n",
    "        res = []\n",
    "        for i in range(0,k-1):\n",
    "            for j in range(i+1,k):\n",
    "                if nums[i] == nums[j]:\n",
    "                    res.append((i,j))\n",
    "                    print (res)\n",
    "        return len(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numIdenticalPairs(self, nums: List[int]) -> int:\n",
    "        count = 0\n",
    "        length = len(nums)\n",
    "        for i in range(length):\n",
    "            for j in range(i + 1, length):\n",
    "                if nums[i] == nums[j]:\n",
    "                    count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numIdenticalPairs(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        res = 0\n",
    "        for i in range(n-1):\n",
    "            for j in range(i+1, n):\n",
    "                if nums[i] == nums[j]:\n",
    "                    res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numIdenticalPairs(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        if n == 1:\n",
    "            return 0\n",
    "        ans = 0\n",
    "        for i in range(n - 1):\n",
    "            for j in range(i + 1, n):\n",
    "                if nums[i] == nums[j]:\n",
    "                    ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numIdenticalPairs(self, nums: List[int]) -> int:\n",
    "        cnt = Counter(nums)\n",
    "        return sum(val * (val - 1) // 2 for val in cnt.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numIdenticalPairs(self, nums: List[int]) -> int:\n",
    "        n=0\n",
    "        newnums=[]\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(len(nums)):\n",
    "                if nums[i]==nums[j] and i<j:\n",
    "                    newnums.append([i,j])\n",
    "                    n+=1\n",
    "        print(newnums)\n",
    "        return n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numIdenticalPairs(self, nums: List[int]) -> int:\n",
    "        combinations = list(itertools.combinations(nums, 2))\n",
    "        ident_combs = [x for x in combinations if x[0]==x[1]]\n",
    "        return len(ident_combs)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numIdenticalPairs(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i + 1, len(nums)):\n",
    "                if nums[i] == nums[j]:\n",
    "                    ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numIdenticalPairs(self, nums: List[int]) -> int:\n",
    "        #暴力法, 输出好数对数目.\n",
    "        res =0;\n",
    "        for i in range(len(nums)-1):\n",
    "            for j in range(i+1,len(nums)):\n",
    "                if nums[i]==nums[j]:\n",
    "                    res+=1;\n",
    "        return res;\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numIdenticalPairs(self, nums: List[int]) -> int:\n",
    "        Hash = {}\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] not in Hash:\n",
    "                Hash[nums[i]] = 1\n",
    "            else:\n",
    "                Hash[nums[i]] += 1\n",
    "        ans = 0\n",
    "        for i in Hash:\n",
    "            n = Hash[i]\n",
    "            ans += n*(n-1)//2\n",
    "        return ans\n",
    "            \n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
